package testhelper

import (
	"fmt"
	"runtime/debug"
	"testing"
)

type TestHelper struct {
	t *testing.T
}

func (this *TestHelper) Init(t *testing.T) {
	this.t = t
}

func (this *TestHelper) Assert(cond bool, ss string) bool {
	if !cond {
		this.t.Error(ss)
		fmt.Println(string(debug.Stack()))
	}
	return cond
}

func (this *TestHelper) AssertBoolValue(cond bool,value bool) bool {
	if cond!=value {
		this.t.Error(UNMATCHED_RESULT)
		fmt.Println(string(debug.Stack()))
	}
	return cond
}

func (this *TestHelper) AssertIsFalse(cond bool) bool {
	if cond {
		this.t.Error(UNMATCHED_RESULT)
		fmt.Println(string(debug.Stack()))
	}
	return cond
}

func (this *TestHelper) AssertIsTrue(cond bool) bool {
	if !cond {
		this.t.Error(UNMATCHED_RESULT)
		fmt.Println(string(debug.Stack()))
	}
	return cond
}

func (this *TestHelper) ShallCrash(f func(), ss string) {
	broken := false
	func() {
		defer func() {
			if err := recover(); err != nil {
				broken = true
			}
		}()
		f()
	}()
	this.Assert(broken, ss)
}

const UNMATCHED_RESULT = "unmatched result"
